<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: Render component
 *
 * @category   	Joomzf Cms
 * @package    	Component
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 May 27, 2012 - 11:14:59 PM
 */
class Joomzf_Component{
	
	private $_name;
	private $_area;
	private $_path;
	
	private $_link;
	
	public $action;
	public $view;
	public $request;
	public $template;
	
	public function __construct(Joomzf_Controller_Action $action){
		$this->action 		= $action;
		$this->view   		= $this->action->view;
		$this->template 	= $this->view->template;
		$this->request 		= $this->action->getRequest();
		
		
		$this->_area		= $this->action->getArea();
		$defaultName 		= ($this->_area == 0) ? dfFRONTEND_COM : dfBACKEND_COM;
		$this->_name		= $this->request->getParam("option",$defaultName);
		$this->_path		= ($this->_area == 0) ? FRONTEND_CPN_PATH.DS.$this->_name : BACKEND_CPN_PATH.DS.$this->_name; 
		
		$this->_link = new stdClass();
		$this->_link->option    = $this->request->getParam("option",$defaultName); 
		$this->_link->control 	= $this->request->getParam("control","index");
		$this->_link->activity  = $this->request->getParam("activity","index");
		$this->_link->renderView  = $this->_link->activity;
		
		Zend_Registry::set("comUrl", $this->_link);
	}
	public function isEnable(){
		$db = Zend_Registry::get("DBO");
		$sql = $db->select()->from(array("cpn"=>TBL_PRF."components"),array("enabled"))->where("cpn.link = ?",'option/'.$this->_name);
		$result = $db->fetchOne($sql);
		if($result == 0) return false;
		return true;
	}
	public function render(){
		$comString = "";
		if($this->isEnable()){
			if(file_exists($this->_path.DS.'index.php')){
				ob_start();
				require($this->_path.DS.'index.php');
				$comString = ob_get_contents();
				ob_clean();
			}
		}
		return $comString;
	}
	public static function getLayout(Joomzf_Component $component){
		$templatePath = $component->template->path;
		$tpath 		  = $templatePath.DS.'components'.DS.$component->getName().DS.$component->getLinkControl().DS.'tmpl'.DS.$component->getLinkRenderView().'.php';
		$cpath 		  = $component->getPath().DS.'views'.DS.$component->getLinkControl().DS.'tmpl'.DS.$component->getLinkRenderView().'.php';
		if (file_exists($tPath)) {
			return $tPath;
		} else{
			return $cpath;
		}
	}
	
	public function getLink(){
		return $this->_link;
	}
	public function getLinkOption(){
		return $this->getName();
	}
	public function getLinkControl(){
		return $this->_link->control;
	}
	public function getLinkActivity(){
		return $this->_link->activity;
	}
	public function getLinkRenderView(){
		return $this->link->renderView;
	}
	public function setLinkRenderView($view){
		$this->link->renderView = $view;
	}
	
	public function getName(){
		return $this->_name;
	}
	public function getPath(){
		return $this->_path;
	}
	
}